############################################################
# ##例子7-0：SHIBOR数据代入Vasicek模型
# getwd()
# mydata=read.csv('shibor.csv')
# mydata=mydata[,c(-3,-4,-5,-6,-7)]
# colnames(mydata)=c('date','rate')
# x=mydata[1:186,2]
# y=diff(x)
# x=x[-186]
# lm01=lm(y~x)
# summary(lm01)
# plot(1:185,x,'l')
# #plot(mydata)

# ############################################################
##例子7-2：收益率曲线拟合
myT=c(1,2,3,5,7,10,14)
myR=c(3.51, 4.54, 5.21, 6.46, 7.26, 7.99, 8.30)*0.01
mydata=data.frame(myT=myT,myR=myR)
lm01=lm(myR~myT+I(myT^2)) #多项式回归
summary(lm01)
pred.frame=data.frame(myT=seq(0,15,0.5))
pp=predict(lm01,newdata=pred.frame,interval='pred') #预测
plot(myT,myR,xlab='T',ylab='R(0,T)',xlim=c(0,15),
     ylim=range(pp))
matlines(pred.frame$myT,pp,lty=c(1,2,2),col='red')
P05=exp(-5*pp[which(pred.frame$myT==5),1]) #找T=5时的预测值
print(P05)
